package loja.dao.impl.hibernate;

import java.util.List;

import loja.dao.despesa.cartao.CartaoCreditoCompraDAO;
import loja.model.conta.titular.Titular;
import loja.model.despesa.cartao.CartaoCreditoCompra;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class CartaoCreditoCompraDAOHibernate implements CartaoCreditoCompraDAO {

	private Session session;

	public CartaoCreditoCompraDAOHibernate(Session session) {
		this.session = session;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	private Criteria createCriteria() {
		return this.session.createCriteria(CartaoCreditoCompra.class);
	}

	@Override
	public void inlcuir(CartaoCreditoCompra cartaoCreditoCompra) {
		this.session.save(cartaoCreditoCompra);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CartaoCreditoCompra> listar() {
		return createCriteria().list();
	}

	@Override
	public CartaoCreditoCompra consultarPorId(Long id) {
		return (CartaoCreditoCompra) createCriteria()
				.add(Restrictions.idEq(id)).uniqueResult();
	}

	@Override
	public void remover(CartaoCreditoCompra cartaoCreditoCompra) {
		this.session.delete(cartaoCreditoCompra);

	}

	@Override
	public void alterar(CartaoCreditoCompra cartaoCreditoCompra) {
		this.session.update(cartaoCreditoCompra);

	}

	@Override
	public CartaoCreditoCompra consultarPorTitular(Titular titular) {
		return (CartaoCreditoCompra) createCriteria().add(
				Restrictions.like("titular", titular)).uniqueResult();
	}

}
